Completed
Push — master ( 39b560...f48291 )
by greg
01:42
created

Builder.js ➔ ???   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
dl 0
loc 46
rs 8.9411
nop 4

1 Function

Rating   Name   Duplication   Size   Complexity  
B Builder.js ➔ ... ➔ build 0 35 4
1
import fse from 'fs-extra'
2
import {saveHtml} from './cms/operations/save'
3
import path from 'path'
4
5
import {
6
  cmsData,
7
  config,
8
  fileAttr,
9
  cmsTemplates,
10
  Page
11
} from './'
12
13
class Builder {
14
15
  constructor(root, folder, dest, flow){
16
    this.pathToJson = path.join(root, config.data.url)
17
    var files = fileAttr.filterLatestVersion(cmsData.file.getFiles(this.pathToJson, config.data.url), flow)
18
19
    if(flow === 'publish') {
20
      files = cmsData.file.getFiles(path.join(root, config.publish.url), new RegExp('.' + config.files.templates.extension))
21
    }
22
23
    var build = function (index) {
24
      var file = files[index]
25
      if(file.path.indexOf('.' + config.files.templates.extension) > -1){
26
        file.path = file.path.replace(config.publish.url, config.data.url)
27
                             .replace('.' + config.files.templates.extension, '.json')
28
        
29
        var json = fse.readJsonSync(file.path)
30
        var text = cmsTemplates.template.getTemplate(json.abe_meta.template)
31
        
32
        cmsData.source.getDataList(path.dirname(json.abe_meta.link), text, json)
33
          .then(() => {
34
            var page = new Page(json.abe_meta.template, text, json, true)
35
            saveHtml(path.join(root, dest + json.abe_meta.link), page.html)
36
            if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
37
          }).catch(function(e) {
38
            console.error(e)
39
            if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
40
          })
41
      }
42
      else if(file.path.indexOf('.json') > -1){
43
        var json = fse.readJsonSync(file.path)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable json already seems to be declared on line 29. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
44
        var text = cmsTemplates.template.getTemplate(json.abe_meta.template)
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable text already seems to be declared on line 30. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
45
46
        cmsData.source.getDataList(path.dirname(json.abe_meta.link), text, json)
47
          .then(() => {
48
            var page = new Page(json.abe_meta.template, text, json, true)
49
            saveHtml(path.join(root, dest + json.abe_meta.link), page.html)
50
            if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
51
          }).catch(function(e) {
52
            console.error(e)
53
            if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
54
          })
55
      }
56
      else if(files[index + 1]) build(index + 1)
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
57
    }
58
59
    build(0)
60
  }
61
}
62
63
if(process.env.ROOT && process.env.FOLDER && process.env.DEST){
64
  config.set({root: process.env.ROOT})
65
  var dest = process.env.DEST || 'tmp'
66
  var flow = process.env.FLOW || 'draft'
67
  new Builder(process.env.ROOT, process.env.FOLDER, dest, flow)
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new Builder(process.env....env.FOLDER, dest, flow) is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
68
}
69